package com.acme.iolab.serialization;


import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

public class ObjectClient {

    public static void main(String[] args) throws IOException {

        try (Socket socket = new Socket(ConnectionSettings.SERVER_IP, ConnectionSettings.SERVER_PORT);
             ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
             ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream())) {

            long start = System.currentTimeMillis();
            for (int i = 0; i < ConnectionSettings.OBJECTS_COUNT; i++) {
                objectOutputStream.writeObject(ConnectionSettings.SERIALIZER.serialize(SensorData.generate()));
            }
            objectInputStream.readObject();
            long elapsed = (System.currentTimeMillis() - start);
            System.out.println("throughput: " + 1d * ConnectionSettings.OBJECTS_COUNT / elapsed);


            start = System.currentTimeMillis();
            objectOutputStream.writeObject(ConnectionSettings.SERIALIZER.serialize(SensorData.generate()));
            objectInputStream.readObject();
            elapsed = (System.currentTimeMillis() - start);
            System.out.println("delay: " + elapsed);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

}
